/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            APP_AutoLamps.c
* version:         1.0
* created_by:      Continental
* date_created:    June 7 2011
*=============================================================================*/
/* DESCRIPTION : Source file for Application layer for AUTOLAMPS              */
/*============================================================================*/
/* FUNCTION COMMENT : Describes the functions for Application Layer for       */
/* AUTOLAMPS                                                                  */
/* Include the state machine for AUTOLAMPS                                    */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 07/jun/2011 |                               | Continental      */
/* Initial version for the Lab 2 using Freescale Bolero micro.                */                                                              
/*============================================================================*/

#include "APP_AutoLamps.h"

#include "sysinit.h"

#include "HAL_LightSensor.h"
#include "HAL_led.h"
#include "HAL_selector.h"


static T_UBYTE SMcurrentState;

void APP_AutoLamps_Init(void)
{
	HAL_led_Init();
	HAL_lightSensor_Init();
	HAL_selector_Init();
	
	/* Initialize AUTOLAMPS state machine */
	SM_AUTOLAMPS(LIGHTS_OFF);
	SMcurrentState = LIGHTS_OFF;
}

void SM_AUTOLAMPS(E_SM_AUTOLAMPS e_sm_autolamps)
{
	switch(e_sm_autolamps)
	{
		case LIGHTS_OFF:
		{
			APP_AutoLamps_Headlamps_Off();
			APP_AutoLamps_Parklamps_Off();
			
			SMcurrentState = LIGHTS_OFF;
			break;
		}
		case LIGHTS_ON:
		{
			APP_AutoLamps_Headlamps_On();
			APP_AutoLamps_Parklamps_On();	
			
			SMcurrentState = LIGHTS_ON;		
			break;
		}
		case HALF_LIGHT:
		{
			APP_AutoLamps_Headlamps_Off();
			APP_AutoLamps_Parklamps_On();
			
			SMcurrentState = HALF_LIGHT;
			break;
		}
		default:
			break;
	}
		
}

T_UBYTE SM_AutoLamps_NextState(void)
{
	T_UBYTE selector_value;
	T_UWORD lightSensor_value;
	T_UBYTE lightSensor_percentage;
	
	T_UBYTE nextState = SMcurrentState;
	
		
	HAL_lightSensor_SendReadRequest();
	
	selector_value = HAL_selector_GetValue();
	
	while( !(HAL_lightSensor_ReadDone()) )
	{
		/* wait for a valid value of light sensor */
	}
	lightSensor_value = HAL_lightSensor_GetValue();
	lightSensor_percentage = APP_AutoLamps_lightPercentage(lightSensor_value);
	
	switch(SMcurrentState)
	{
		case LIGHTS_OFF:
		{
			if(selector_value == SELECTOR_PARKLAMPS)
			{
				nextState = HALF_LIGHT;
				APP_AutoLamps_HeadlampsPwm();
			}
			else if(selector_value == SELECTOR_HEADLAMPS)
			{
				nextState = LIGHTS_ON;
			}
			else if((selector_value == SELECTOR_AUTO) && (lightSensor_percentage < 40) )
			{
				nextState = LIGHTS_ON;
			}
			
			break;
		}
		case LIGHTS_ON:
		{
			if(selector_value == SELECTOR_OFF)
			{
				nextState = LIGHTS_OFF;
				APP_AutoLamps_BothlampsPwm();
			}
			else if(selector_value == SELECTOR_PARKLAMPS)
			{
				nextState = HALF_LIGHT;
				APP_AutoLamps_HeadlampsPwm();
			}
			else if((selector_value == SELECTOR_AUTO) && (lightSensor_percentage > 60) )
			{
				nextState = HALF_LIGHT;
				APP_AutoLamps_HeadlampsPwm();
			}
					
			break;
		}
		case HALF_LIGHT:
		{
			if(selector_value == SELECTOR_HEADLAMPS)
			{
				nextState = LIGHTS_ON;
			}
			else if(selector_value == SELECTOR_OFF)
			{
				nextState = LIGHTS_OFF;
				APP_AutoLamps_BothlampsPwm();
			}
			else if((selector_value == SELECTOR_AUTO) && (lightSensor_percentage > 80) )
			{
				nextState = LIGHTS_OFF;
				APP_AutoLamps_BothlampsPwm();
			}
			
			break;
		}
		default:
			break;
	}
	
	return nextState;
}

T_UBYTE APP_AutoLamps_lightPercentage(T_UWORD sensor_value)
{
	T_UWORD percentage;
	
	percentage = (sensor_value * 100) / 1023;
	
	return (T_UBYTE)percentage;
}

void APP_AutoLamps_BothlampsPwm(void)
{
	T_UWORD counter;
	T_UBYTE duty_cycle;
	
	
	MAL_pwm_InitPWM(LED1_PWM_CH, LED1);
	MAL_pwm_InitPWM(LED2_PWM_CH, LED2);
	MAL_pwm_InitPWM(LED3_PWM_CH, LED3);
	MAL_pwm_InitPWM(LED4_PWM_CH, LED4);
	
	for(duty_cycle = 10; duty_cycle < 100; duty_cycle += 2)
	{
		MAL_pwm_SetDutyCycle(LED1_PWM_CH, duty_cycle);
		MAL_pwm_SetDutyCycle(LED2_PWM_CH, duty_cycle);
		MAL_pwm_SetDutyCycle(LED3_PWM_CH, duty_cycle);
		MAL_pwm_SetDutyCycle(LED4_PWM_CH, duty_cycle);
		
		for(counter = 0; counter < 500000; counter++)
		{
			
		}
	}
	
	MALbeMIOS_deinitEMIOS0_ch(LED1_PWM_CH);
	MALbeMIOS_deinitEMIOS0_ch(LED2_PWM_CH);
	MALbeMIOS_deinitEMIOS0_ch(LED3_PWM_CH);
	MALbeMIOS_deinitEMIOS0_ch(LED4_PWM_CH);
	MALbeMIOS_deinitEMIOS0();
	/* Set off LED's again */
	HAL_led_Init();
}

/* Headlamps */
void APP_AutoLamps_HeadlampsPwm(void)
{
	T_UWORD counter;
	T_UBYTE duty_cycle;
	
	
	MAL_pwm_InitPWM(LED1_PWM_CH, LED1);
	MAL_pwm_InitPWM(LED2_PWM_CH, LED2);
	
	for(duty_cycle = 10; duty_cycle < 100; duty_cycle += 2)
	{
		MAL_pwm_SetDutyCycle(LED1_PWM_CH, duty_cycle);
		MAL_pwm_SetDutyCycle(LED2_PWM_CH, duty_cycle);
		
		for(counter = 0; counter < 500000; counter++)
		{
			
		}
	}
	
	MALbeMIOS_deinitEMIOS0_ch(LED1_PWM_CH);
	MALbeMIOS_deinitEMIOS0_ch(LED2_PWM_CH);
	MALbeMIOS_deinitEMIOS0();
	/* Set off LED's again */
	HAL_led_Init();
}

void APP_AutoLamps_Headlamps_On(void)
{
	HAL_led_TurnOn(LED1);
	HAL_led_TurnOn(LED2);	
}

void APP_AutoLamps_Headlamps_Off(void)
{
	HAL_led_TurnOff(LED1);
	HAL_led_TurnOff(LED2);
}

/* Parklamps */
void APP_AutoLamps_ParklampsPwm(void)
{
	T_UWORD counter;
	T_UBYTE duty_cycle;
	
	
	MAL_pwm_InitPWM(LED3_PWM_CH, LED3);
	MAL_pwm_InitPWM(LED4_PWM_CH, LED4);
	
	for(duty_cycle = 10; duty_cycle < 100; duty_cycle += 2)
	{
		MAL_pwm_SetDutyCycle(LED3_PWM_CH, duty_cycle);
		MAL_pwm_SetDutyCycle(LED4_PWM_CH, duty_cycle);
		
		for(counter = 0; counter < 500000; counter++)
		{
			
		}
	}
	
	MALbeMIOS_deinitEMIOS0_ch(LED3_PWM_CH);
	MALbeMIOS_deinitEMIOS0_ch(LED4_PWM_CH);
	MALbeMIOS_deinitEMIOS0();
	/* Set off LED's again */
	HAL_led_Init();
}

void APP_AutoLamps_Parklamps_On(void)
{
	HAL_led_TurnOn(LED3);
	HAL_led_TurnOn(LED4);	
}

void APP_AutoLamps_Parklamps_Off(void)
{
	HAL_led_TurnOff(LED3);
	HAL_led_TurnOff(LED4);	
}